﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using Palsta.Core.data;
using Palsta.Core.function;

namespace Palsta.Core.exp
{
    class ExpFunctionNode : ExpNode
    {
        #region Members

        private String name;
        private ArrayList args;

        #endregion

        #region Constructors

        #endregion

        #region Accessors

        public virtual String Name
        {
            get
            {
                return this.name;
            }
            set
            {
                this.name = value;
            }
        }

        public virtual ArrayList Args
        {
            get
            {
                return this.args;
            }
            set
            {
                this.args = value;
            }
        }

        #endregion

        #region Methods

        public ExpNodeType GetNodeType()
        {
            return ExpNodeType.Function;
        }

        public bool Test(DataDictionary data)
        {
            Field f = this.GetValue(data);
            if (f == null)
            {
                return false;
            }
            if (f.GetFieldType() == FieldType.Boolean)
            {
                return ((BooleanField)f).GetBooleanValue();
            }
            else
            {
                return true;
            }
        }

        public Field GetValue(DataDictionary data)
        {
            if (this.args != null)
            {
                Field[] argValues = new Field[this.args.Count];
                for (int i = 0; i < this.args.Count; i++)
                {
                    argValues[i] = ((ExpNode)this.args[i]).GetValue(data);
                }
                return XsltFunctionManager.ExecuteFunction(this.name, argValues);
            }
            else
            {
                return XsltFunctionManager.ExecuteFunction(this.name, null);
            }
        }

        public FieldType GetFieldType(DataDictionary data)
        {
            return XsltFunctionManager.GetReturnType(this.name);
        }

        public void AddArg(ExpNode n)
        {
            if (this.args == null)
            {
                this.args = new ArrayList();
            }
            this.args.Add(n);
        }

        public ArrayList GetArgs()
        {
            return this.args;
        }

        public String GetName()
        {
            return this.name;
        }

        public void SetName(String n)
        {
            this.name = n;
        }
        #endregion

        #region Override Operators

        #endregion

        #region Static Methods

        #endregion

    }


}
